package com.probox.modules.controller.product;


import cn.hutool.core.util.ObjectUtil;
import com.probox.common.core.domain.R;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.enums.ProductAuditStatus;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.core.web.page.TableDataInfo;
import com.probox.common.log.annotation.Log;
import com.probox.common.log.enums.BusinessType;
import com.probox.common.security.annotation.PreAuthorize;
import com.probox.common.entity.domain.product.ProductInfo;
import com.probox.common.entity.domain.product.vo.*;
import com.probox.common.entity.service.product.IProductInfoService;
import com.probox.common.entity.service.product.IProductLogService;
import com.probox.common.entity.service.product.IProductUnitService;
import com.probox.product.api.domain.ProductInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 商品信息Controller
 *
 * @date 2021-05-28
 */
@Api(value = "商品模块", tags = {"商品信息后端接口"})
@RestController
@RequestMapping("/product/info")
public class ProductInfoController extends BaseController {
    @Autowired
    private IProductInfoService productInfoService;

    @Autowired
    private IProductLogService iProductLogService;


    /**
     * 获取分类
     *
     * @return
     */
    @GetMapping("/cate")
    public AjaxResult get() {
        return AjaxResult.success(productInfoService.category());
    }

    /**
     * 添加测试数据
     *
     * @param list
     * @return
     */
    @PostMapping("/test/goods")
    public AjaxResult addTestGoods(@RequestBody List<PddCategory> list) {
        return AjaxResult.success(productInfoService.goods(list));
    }

    /**
     * 查询商品信息列表
     */
    @PreAuthorize(hasPermi = "product:info:list")
    @GetMapping("/list")
    public TableDataInfo list(ProductInfoQuery productInfoQuery) {
        startPage();
        List<ProductInfoVo> list = productInfoService.queryList(productInfoQuery);
        TableDataInfo rst = getDataTable(list);
        rst.setTotal(productInfoService.selectCountAdmin(productInfoQuery));
        return rst;
    }

    @ApiOperation(value = "论文资料信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductInfoVo.class),
    })
    @GetMapping("/goods/list")
    public AjaxResult selectInfo(ProductInfoQuery productInfoQuery) {
        productInfoQuery.setPlatformUid(SecurityUtils.getPlatformId());
        productInfoQuery.setDataStatus(DataStatus.valid.getKey());
        startPage();
        List<ProductInfoVo> list = productInfoService.queryList(productInfoQuery);
        return AjaxResult.successs(list, productInfoService.selectCountAdmin(productInfoQuery));
    }

    /**
     * 查询商品日志信息列表
     */
    @GetMapping("/log")
    public TableDataInfo list(ProductLogQuery productLogQuery) {
        startPage();
        List<ProductLogVo> list = iProductLogService.queryList(productLogQuery);
        return getDataTable(list);
    }

    /**
     * 查询商品信息列表
     */
    @PreAuthorize(hasPermi = "product:info:warning:query")
    @GetMapping("/warning")
    public TableDataInfo queryWarning(ProductInfoQuery productInfoQuery) {
        productInfoQuery.setDataStatus(DataStatus.valid.getKey());
        productInfoQuery.setAuditStatus(ProductAuditStatus.pass.getKey());
        productInfoQuery.setWarningQuery("123");
        startPage();
        List<ProductInfoVo> list = productInfoService.queryList(productInfoQuery);
        return getDataTable(list);
    }


    /**
     * 获取商品信息详细信息
     */
    @PreAuthorize(hasPermi = "product:info:query")
    @GetMapping(value = "/{productUid}")
    public AjaxResult getInfo(@PathVariable("productUid") Long productUid) {
        return AjaxResult.success(productInfoService.getSaveVo(productUid));
    }

    @PreAuthorize(hasPermi = "product:info:query")
    @GetMapping(value = "/detail/{productUid}")
    public AjaxResult getdetial(@PathVariable("productUid") Long productUid) {
        return AjaxResult.success(productInfoService.getViewVo(productUid));
    }

    @GetMapping(value = "/detailEdit/{productUid}")
    public AjaxResult getdetialEdit(@PathVariable("productUid") Long productUid) {
        AjaxResult ajax = new AjaxResult();
        ajax.put("data", productInfoService.queryEditVo(productUid));
        return ajax;
    }


    /**
     * 新增商品信息
     */
    @PreAuthorize(hasPermi = "product:info:add")
    @Log(title = "论文资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.insertProductInfo(productSaveVo, 0L));
    }

    /**
     * 一键发布
     *
     * @param productSaveVo
     * @return
     */
    @PreAuthorize(hasPermi = "product:info:grounding")
    @PostMapping("/grounding")
    public AjaxResult getGrounding(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.insertProductInfo(productSaveVo, 1L));
    }

    /**
     * 一键发布
     *
     * @param productSaveVo
     * @return
     */
    @PreAuthorize(hasPermi = "product:info:grounding")
    @PutMapping("/grounding")
    public AjaxResult getGroundings(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.updateProductInfo(productSaveVo, 1L));
    }

    /**
     * 保存并提交
     *
     * @param productSaveVo
     * @return
     */
    @PreAuthorize(hasPermi = "product:info:edit")
    @PostMapping("/saveAndAudit")
    public AjaxResult saveAndAudit(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.insertProductInfo(productSaveVo, 2L));
    }

    /**
     * 保存并提交
     *
     * @param productSaveVo
     * @return
     */
    @PreAuthorize(hasPermi = "product:info:edit")
    @PutMapping("/saveAndAudit")
    public AjaxResult saveAndAuditPut(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.updateProductInfo(productSaveVo, 2L));
    }

    /**
     * 修改商品信息
     */
    @PreAuthorize(hasPermi = "product:info:edit")
    @Log(title = "论文资料信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSaveVo productSaveVo) {
        return toAjax(productInfoService.updateProductInfo(productSaveVo, 0l));
    }

    /**
     * 修改价格和库存
     *
     * @param vo
     * @return
     */
    @Log(title = "论文资料信息", businessType = BusinessType.UPDATE)
    @PutMapping("detailEdit")
    public AjaxResult edit(@RequestBody ProductEditAllVo vo) {
        return toAjax(productInfoService.updateEdit(vo));
    }

    /**
     * 日志操作
     *
     * @param productInfoEditVo
     * @return
     */
    @Log(title = "论文资料信息", businessType = BusinessType.UPDATE)
    @PutMapping("editSaveLog")
    public AjaxResult editSaveLog(@RequestBody ProductInfoEditVo productInfoEditVo) {
        return toAjax(productInfoService.editAndSaveLog(productInfoEditVo));
    }


    /**
     * 删除商品信息
     */
    @PreAuthorize(hasPermi = "product:info:remove")
    @Log(title = "论文资料信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{productUids}")
    public AjaxResult remove(@PathVariable Long[] productUids) {
        return toAjax(productInfoService.deleteProductInfoByIds(productUids));
    }

    /**
     * 根据id集，查询
     *
     * @param proUids
     * @return
     */
    @GetMapping("/infoList/{proUids}")
    public R<List<ProductInfoVo>> getProList(@PathVariable("proUids") String proUids) {
        return R.ok(productInfoService.getProductList(proUids));
    }

    /**
     * 远程调用
     *
     * @param productUid
     * @return
     */
    @GetMapping("/infoDetail/{productUid}")
    public R<ProductInfoVo> getPro(@PathVariable("productUid") Long productUid) {
        ProductInfoVo productInfoVo = new ProductInfoVo();
        ProductViewVo viewVo = productInfoService.getViewVo(productUid);
        if (ObjectUtil.isEmpty(viewVo)) return R.ok();
        BeanUtils.copyProperties(viewVo, productInfoVo);
        return R.ok(productInfoVo);
    }

    /**
     * 查询单位是否商品引用
     *
     * @param unit
     * @return
     */
    @GetMapping("/unit/use/{unit}")
    public AjaxResult productUnitUse(@PathVariable("unit") Long unit) {
        return AjaxResult.success(productInfoService.productUnitUse(unit));
    }

    /**
     * 获取分享
     *
     * @param productInfo
     * @return
     */
    @PostMapping("/share/Img")
    public AjaxResult appletImg(@RequestBody ProductInfo productInfo) {
        AjaxResult ax = new AjaxResult();
        HashMap h5 = productInfoService.getH5(productInfo.getProductUid());
        ax.put("url", h5.get("url"));
        ax.put("h5Url", h5.get("urlCode"));
        ax.put("codeUrl", productInfoService.getAppltImg(productInfo.getProductUid(), SecurityUtils.getPlatformId()));
        return ax;
    }

    /**
     * 获得商品编码
     *
     * @return
     */
    @GetMapping("/productNo")
    public AjaxResult getNo() {
        long l = System.currentTimeMillis();
        String no = "p" + String.valueOf(l);
        return AjaxResult.success(no);
    }


    @ApiOperation(value = "获取小程序二维码")
    @PostMapping("/code")
    public AjaxResult getCodeAddress(@RequestBody ProductInfoQuery query) {
        return AjaxResult.success(productInfoService.getAppltImg(query.getProductUid(), SecurityUtils.getPlatformId()));
    }

}
